<template>
  <div :class="['chat-message', type]">
    <div class="avatar">
      <el-avatar :size="32">
        {{ type === 'user' ? '用户' : 'AI' }}
      </el-avatar>
    </div>
    <div class="message-content">
      <div v-if="type === 'ai'" v-html="renderedContent" class="markdown-content"></div>
      <div v-else class="text-content">{{ content }}</div>
      <!-- 渲染图片列表 -->
      <div v-if="imageList.length" class="message-images">
        <div v-for="(image, index) in imageList" :key="index" class="image-item">
          <el-image 
            :src="image.url" 
            :alt="image.description"
            :preview-src-list="imageList.map(img => img.url)"
            :initial-index="index"
            fit="cover"
            class="content-image"
            @click="$emit('image-preview', image.url, imageList.map(img => img.url))"
          >
            <template #error>
              <div class="image-error">
                <el-icon><Picture /></el-icon>
                <span>图片加载失败</span>
              </div>
            </template>
          </el-image>
          <div v-if="image.description" class="image-description">{{ image.description }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue';
import { renderMarkdown } from '@/utils/markdownRender';
import { defineProps, defineEmits } from 'vue';
import { ElImage } from 'element-plus';
import { Picture } from '@element-plus/icons-vue';

// const props = defineProps({
//   type: {
//     type: String,
//     default: 'user', // 'user' 或 'ai'
//     validator: value => ['user', 'ai'].includes(value)
//   },
//   content: {
//     type: String,
//     default: ''
//   },
//   streaming: {
//     type: Boolean,
//     default: false
//   }
// });


const props = defineProps({
  type: String,
  content: String,
  imageList: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits(['image-preview']);

const renderedContent = ref('');

// 渲染Markdown内容
const updateRenderedContent = async () => {
  if (props.type === 'ai') {
    renderedContent.value = await renderMarkdown(props.content);
  }
};

watch(() => props.content, updateRenderedContent);
onMounted(updateRenderedContent);
</script>

<style scoped>
.chat-message {
  display: flex;
  margin-bottom: 20px;
  padding: 16px;
  border-radius: 8px;
}

.chat-message.user {
  background-color: #f5f7fa;
  flex-direction: row-reverse;
}

.chat-message.ai {
  background-color: #ffffff;
  border: 1px solid #e0e0e0;
}

.avatar {
  margin: 0 12px;
}

.message-content {
  flex: 1;
}

.text-content {
  white-space: pre-wrap;
  word-break: break-word;
}

.markdown-content {
  white-space: normal;
  word-break: break-word;
}

.markdown-content :deep(*) {
  margin: 0.5em 0;
}

.markdown-content :deep(h1),
.markdown-content :deep(h2),
.markdown-content :deep(h3) {
  margin: 1em 0 0.5em 0;
  font-weight: 600;
}

.markdown-content :deep(code) {
  background-color: #f3f4f6;
  padding: 0.2em 0.4em;
  border-radius: 3px;
  font-family: monospace;
}

.markdown-content :deep(pre) {
  background-color: #f8f9fa;
  padding: 1em;
  border-radius: 5px;
  overflow-x: auto;
}

.markdown-content :deep(blockquote) {
  border-left: 4px solid #e0e0e0;
  padding-left: 1em;
  margin-left: 0;
  color: #666;
}


.message-images {
  margin-top: 12px;
}

.image-item {
  margin-bottom: 16px;
}

.content-image {
  max-width: 100%;
  max-height: 300px;
  border-radius: 8px;
  cursor: pointer;
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100px;
  color: #999;
}

.image-description {
  margin-top: 8px;
  font-size: 12px;
  color: #666;
  text-align: center;
}
</style>